package tool.math

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.runBlocking
import org.jetbrains.compose.resources.getString
import org.jetbrains.compose.resources.stringResource
import tool.Tool
import tool.ToolEvent
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.*
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.Res
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.binary
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.number_base_converter
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.octal
import java.math.BigInteger

object NumberBaseConverterTool : Tool {

    private val _name: String by lazy {
        runBlocking {
            getString(Res.string.number_base_converter)
        }
    }

    override fun getName(): String {
        return _name
    }

    @Composable
    override fun Render(onEvent: (ToolEvent) -> Unit) {
        Column {
            Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                IconButton(onClick = { onEvent(ToolEvent.BackEvent) }) {
                    Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = null)
                }
                Text(text = getName(), style = MaterialTheme.typography.headlineSmall)
            }
            Box(
                modifier = Modifier.fillMaxSize()
                    .padding(top = 8.dp)
                    .background(brush = SolidColor(MaterialTheme.colorScheme.surface))
                    .border(
                        width = 1.dp,
                        color = MaterialTheme.colorScheme.outlineVariant,
                        shape = MaterialTheme.shapes.large
                    )
            ) {
                Column(
                    modifier = Modifier.padding(16.dp)
                ) {
                    val bigInteger: MutableState<BigInteger?> = remember { mutableStateOf(BigInteger.valueOf(2)) }
                    fun onChange(change: String, radix: Int) {
                        if (change == "") {
                            bigInteger.value = null
                        } else {
                            val new = kotlin.runCatching { BigInteger(change, radix) }
                            if (new.isSuccess) {
                                bigInteger.value = new.getOrNull()
                            }
                        }
                    }
                    Text(stringResource(Res.string.binary), modifier = Modifier.padding(top = 10.dp, bottom = 5.dp))
                    OutlinedTextField(
                        value = bigInteger.value?.toString(2) ?: "",
                        onValueChange = { onChange(it, 2) },
                        maxLines = 1,
                        modifier = Modifier.fillMaxWidth()
                    )
                    Text(stringResource(Res.string.octal), modifier = Modifier.padding(top = 10.dp, bottom = 5.dp))
                    OutlinedTextField(
                        value = bigInteger.value?.toString(8) ?: "",
                        onValueChange = { onChange(it, 8) },
                        maxLines = 1,
                        modifier = Modifier.fillMaxWidth()
                    )
                    Text(stringResource(Res.string.decimal), modifier = Modifier.padding(top = 10.dp, bottom = 5.dp))
                    OutlinedTextField(
                        value = bigInteger.value?.toString() ?: "",
                        onValueChange = { onChange(it, 10) },
                        maxLines = 1,
                        modifier = Modifier.fillMaxWidth()
                    )
                    Text(stringResource(Res.string.hexadecimal), modifier = Modifier.padding(top = 10.dp, bottom = 5.dp))
                    OutlinedTextField(
                        value = bigInteger.value?.toString(16) ?: "",
                        onValueChange = {
                            onChange(it, 16)
                        },
                        maxLines = 1,
                        modifier = Modifier.fillMaxWidth()
                    )
                    Text(stringResource(Res.string.thirty_two_base), modifier = Modifier.padding(top = 10.dp, bottom = 5.dp))
                    OutlinedTextField(
                        value = bigInteger.value?.toString(32) ?: "",
                        onValueChange = {
                            onChange(it, 32)
                        },
                        maxLines = 1,
                        modifier = Modifier.fillMaxWidth()
                    )
                }
            }
        }
    }
}